package com.dazhewan.project.component;

import com.dazhewan.project.properties.GlobalSettting;
import com.dazhewan.utils.FunctionUtil;
import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatIntelliJLaf;
import com.formdev.flatlaf.FlatLightLaf;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @author: zheng
 * @code date: 2022/10/10
 * @code className: SettingDialog
 * @code description: 设置弹出框
 */
public class SettingDialog {
    private JFrame frame;
    private JDialog settingDialog = null;

    public SettingDialog(JFrame frame) {
        this.settingDialog = new JDialog(frame, "设置", true);
        try {
            settingDialog.setIconImage(new ImageIcon(FunctionUtil.getRootPath() + "\\icon\\setting.png").getImage());
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.frame = frame;
        //初始化弹窗容器
        initDialog();
        FunctionUtil.addContainer(settingDialog);
        addComponent();
    }

    /**
     * @method: 添加左侧操作菜单
     */
    private void addComponent() {
        final JPanel panel = new JPanel();
        panel.setBorder(new EmptyBorder(5,10,10,5));
        FunctionUtil.addContainer(panel);

        JTabbedPane jTabbedPane = new JTabbedPane(SwingConstants.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
        FunctionUtil.addContainer(jTabbedPane);
        jTabbedPane.setPreferredSize(new Dimension(settingDialog.getWidth(), settingDialog.getHeight()));

        jTabbedPane.addTab("界面风格", getMainStyle());
        jTabbedPane.addTab("基础设置", null);
        jTabbedPane.addTab("接口设置", null);
        jTabbedPane.setSelectedIndex(0);//设置默认选中

        panel.add(jTabbedPane);

        settingDialog.add(panel);
    }

    /**
     * @method: 获取界面风格界面
     */
    public JPanel getMainStyle() {
        final JPanel panel = new JPanel();
        panel.setBorder(new EmptyBorder(5,10,5,10));
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        FunctionUtil.addContainer(panel);
        String lightStr = "FlatLaf Light 风格",
                drakStr = "FlatLaf Dark 风格",
                intellijStr = "FlatLaf IntelliJ 风格",
                darculaStr = "FlatLaf Darcula 风格";
        final ButtonGroup buttonGroup = new ButtonGroup();
        final ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                final String actionCommand = e.getActionCommand();
                try {
                    if (lightStr.equals(actionCommand)) {
                        UIManager.setLookAndFeel(new FlatLightLaf());
                    } else if (drakStr.equals(actionCommand)) {
                        UIManager.setLookAndFeel(new FlatDarkLaf());
                    } else if (intellijStr.equals(actionCommand)) {
                        UIManager.setLookAndFeel(new FlatIntelliJLaf());
                    } else if (darculaStr.equals(actionCommand)) {
                        UIManager.setLookAndFeel(new FlatDarculaLaf());
                    }
                } catch (Exception ex) {
                    System.err.println("Failed to initialize LaF");
                }
                //刷新外观组件
                FunctionUtil.refreshStyle();
            }

        };
        createJRadioButton(lightStr, buttonGroup, actionListener, panel);
        createJRadioButton(drakStr, buttonGroup, actionListener, panel);
        createJRadioButton(intellijStr, buttonGroup, actionListener, panel);
        createJRadioButton(darculaStr, buttonGroup, actionListener, panel);

        return panel;
    }

    private JRadioButton createJRadioButton(String name, ButtonGroup buttonGroup, ActionListener actionListener, JPanel panel) {
        final JRadioButton radioButton = new JRadioButton(name, checkStyle(name));
        FunctionUtil.addContainer(radioButton);
        radioButton.addActionListener(actionListener);
        buttonGroup.add(radioButton);
        panel.add(radioButton);
        return radioButton;
    }

    private boolean checkStyle(String style) {
        return style.equals(GlobalSettting.STYLE_STR);
    }

    /**
     * @method: 初始化弹窗，大小
     * @param: frame父容器对象
     */
    public void initDialog() {
        settingDialog.setLocation((int) frame.getLocation().getX() + 15, (int) frame.getLocation().getY() + 15);
        settingDialog.setSize(GlobalSettting.MAIN_BUILDER.mainFrame().getWidth() - 30, GlobalSettting.MAIN_BUILDER.mainFrame().getHeight() - 30);
    }

    public JDialog getSettingDialog() {
        initDialog();
        return settingDialog;
    }
}
